package com.idanchuang.component.logback.helper;

import com.alibaba.ttl.TransmittableThreadLocal;
import com.idanchuang.component.core.ServiceInfo;
import com.idanchuang.component.logback.enums.MaskType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author yjy
 * @date 2020/4/21 10:52
 **/
public class LogContext {

    public static final String STAR = "*";
    public static final String INTERNAL_LOGGER_NAME = "componentInternalLog";
    public static final Logger INTERNAL_LOGGER = LoggerFactory.getLogger(INTERNAL_LOGGER_NAME);

    private static final ThreadLocal<String> KEYWORD = new TransmittableThreadLocal<>();
    private static final ThreadLocal<MaskType[]> IGNORE_MASKS = new TransmittableThreadLocal<>();
    private static final ThreadLocal<Boolean> KEEP_ORIGIN = new TransmittableThreadLocal<>();

    public static void ignoreMasksWithKeyword(String keyword, MaskType... maskTypes) {
        setKeyword(keyword);
        ignoreMasks(maskTypes);
    }

    public static void keepOriginWithKeyword(String keyword) {
        setKeyword(keyword);
        keepOrigin();
    }

    /**
     * 设置当前日志关键词
     * @param keyword 关键词
     */
    public static void setKeyword(String keyword) {
        KEYWORD.set(keyword);
    }

    /**
     * 获取当前关键词
     * @return 关键词
     */
    public static String getKeyword() {
        return KEYWORD.get();
    }

    /**
     * 移除当前关键词
     */
    public static void removeKeyword() {
        KEYWORD.remove();
    }

    /**
     * 设置本条日志不做脱敏
     * @param maskTypes 哪些数据类型不做脱敏
     */
    public static void ignoreMasks(MaskType... maskTypes) {
        if (maskTypes == null || maskTypes.length == 0) {
            IGNORE_MASKS.set(new MaskType[]{MaskType.PATTERN_ALL});
        } else {
            IGNORE_MASKS.set(maskTypes);
        }
    }

    /**
     * 获取当前忽略的敏感信息
     * @return 敏感信息类型
     */
    public static MaskType[] getIgnoreMasks() {
        return IGNORE_MASKS.get();
    }

    /**
     * 获取并移除当前忽略的敏感信息
     * @return 敏感信息类型
     */
    public static MaskType[] removeIgnoreMasks() {
        MaskType[] maskTypes = getIgnoreMasks();
        IGNORE_MASKS.remove();
        return maskTypes;
    }

    /**
     * 设置保留本条原始日志
     */
    public static void keepOrigin() {
        KEEP_ORIGIN.set(true);
    }

    /**
     * 获取是否保留本条原始日志
     * @return 是否保留
     */
    public static boolean getKeepOrigin() {
        Boolean keep =  KEEP_ORIGIN.get();
        return keep != null && keep;
    }

    /**
     * 获取并移除是否保留本条原始日志
     * @return 是否保留
     */
    public static boolean removeKeepOrigin() {
        boolean keep = getKeepOrigin();
        KEEP_ORIGIN.remove();
        return keep;
    }

    public static String getAppId() {
        return ServiceInfo.getAppId();
    }

    public static String getAppGroup() {
        return ServiceInfo.getGroup();
    }

    public static String getAppVersion() {
        return ServiceInfo.getVersion();
    }

    public static String getHost() {
        return ServiceInfo.getHost();
    }

    public static String getPort() {
        return ServiceInfo.getPort();
    }

}
